#!/usr/bin/perl

#-------------------------------------------------------------------------------
# csp2cgi.pl
#	convert csp to cgi, include csp compile and cxx compile
# Copyright(C) 2002 - 2005, Leon, ME2Soft. All rights reserved!
#
# $Id: csp2cgi.pl,v 1.11 2008/07/14 09:21:37 leon Exp $
#
# vars:
# $ARGV[0]		the csp dir
# $ARGV[1]		the csp file ext 
# $ARGV[2]		the work dir 
# $SrcDir		the src dir.
# $CSPCompiler 	the csp compiler. eg. "cspc"
# $CSPCXX		the cxx cmd, eg. "g++ -o %o %c -lmysqlclient -lgdbm -lclfc"	
#
# eg.
#		perl csp2cgi.pl /home/me2soft/test/  .csp  /home/me2soft/workdir/test/
#-------------------------------------------------------------------------------

$ddd = rindex($0, "/");
if($ddd>=0) {
	$ddd = substr($0, 0, $ddd+1)."..";
} else {
	$ddd = "..";
}

$SrcDir = $ddd;
$CSPCompiler = "$SrcDir/csp/cspc-fc8";
@CSPC = ("g++ -I$SrcDir -I$ARGV[0] -o ", " -L/usr/local/lib ", " -lmysqlclient -lgdbm $SrcDir/clfc/libclfc-fc8.a ");

$olddir = `pwd`;
chomp($olddir);
chdir($ARGV[0]);
@csp_files = split /\s/, `ls *$ARGV[1]*`;
chdir($olddir);
for($i = 0; $i < @csp_files; $i++) {
	$cmd = "$CSPCompiler $ARGV[0]$csp_files[$i] $ARGV[2]";
	print "\n----------\n$cmd\n";
	$next_cmd = `$cmd`;
	print "$next_cmd\n";
	$flags = "";
	@fff = split /\n/, $next_cmd;
	for($j=1;$j<@fff;$j++) {
		$flags .= $fff[$j];
	}
	$cxx = $CSPC[0].$ARGV[2].$csp_files[$i].$CSPC[1].$ARGV[2].$csp_files[$i].".cpp $flags ".$ARGV[3].$CSPC[2];	
	print $cxx;
	print `$cxx`;
	`strip $ARGV[2]$csp_files[$i]`;
	#unlink("$ARGV[2]$csp_files[$i].cpp");
}
print "\nDone!\n";



